<template>
  <div class="warp" id="app_box">
    <router-view v-slot="{ Component }">
      <keep-alive>
        <component v-if="route.meta.keepAlive" :key="route.name" :is="Component"></component>
      </keep-alive>
      <component v-if="!route.meta.keepAlive" :key="route.name" :is="Component"></component>
    </router-view>
    <van-back-top target=".warp" immediate bottom="100" :offset="400" />
  </div>
  <!-- 签名弹窗 -->
  <van-popup :close-on-click-overlay="false" v-model:show="showSinger" round :style="{ padding: '20px 34px' }">
    <div class="singer_value background_box">{{ t('toast.pleaseSignFirst') }}</div>
    <div class="btn_two">
      <van-button @click="cancelSinger"> {{ t('Cancel') }}</van-button>
      <van-button type="primary" :loading="loading" @click="getSinger">
        {{ t('sign') }}</van-button>
    </div>
  </van-popup>
</template>
<script setup>
import { ethers } from "ethers";
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
import {jsonAbiToHumanReadable} from "../utils/index"
const route = useRoute()
import { ref, onMounted, onUnmounted, computed, watch } from "vue";
import { useI18n } from "vue-i18n";
import { showToast } from "vant";
import { useStore } from "vuex";
import eth from "../utils/eth";
import { switchNetwork } from "@/utils/eth.js";
import loanEth from "@/utils/loanEth.js";
import userEth from "@/utils/userEth.js";
import c2cEth from "@/utils/c2cEth.js";

const store = useStore();
const loading = ref(false);
const showSinger = ref(false);
const active = ref(0);
const showLoading = ref(false)
const { locale, t } = useI18n();
// 使用计算属性实现响应式
const goToPage = (value) => {
  router.push({
    name: value,
  });
};
eth.linkWallet()
const fn = () => {
  if (typeof window.ethereum !== 'undefined' || (typeof window.web3 !== 'undefined')) {
    // 用户正在使用 Web3 浏览器或连接了以太坊网络
  } else {
    // 用户没有连接 Web3 浏览器或以太坊网络
    showToast(t('pleaseInWeb3'))
  }
  ethereum.on("accountsChanged", async function (accounts) {

    if(accounts&&accounts[0]){
      if(store.state.address!=accounts[0].toLowerCase()){
        store.commit('changeAddress', accounts[0].toLowerCase())
        eth.linkWallet()
        initData()
      }
    }
 
  });
  window.ethereum.on("chainChanged", async(chain) => {
    await switchNetwork(chain);
    eth.linkWallet()
  });


};
const getMyInfo = async () => {
  const account = await eth.getAccount();
  let obj = await userEth.getUseInfo(account)
  store.commit('changeMyUserObj', obj)

}
const initData = async ()=>{
  // 获取交易员列表
  userEth.getCashPledge()
  userEth.getApprovedTraders(0)
  await userEth.getApprovedTraders(1)
  userEth.getApprovedTraders(2)
  userEth.getMinFee()

  userEth.getTimeList(0)
  userEth.getTimeList(1)
  // 获取当前交易员详情
  await userEth.getTraderDetail()
  // 判断是否是管理员
  userEth.admins()
  eth.getUsdtAndBtc()
  // 获取全网数据
  loanEth.total()
  c2cEth.total()
  loanEth.getPrice()
  // 获取手续费
  userEth.getPlatformFee()
  // 获取当前用户所有订单
  await loanEth.getUserOrdersDetails()
  // 获取所有挂单
  await loanEth.getPendingLoanOrders()
  await c2cEth.getLatest()
  getMyInfo()

  // loanEth.getDonateByOwner()
  // loanEth.admins()
  // loanEth.getParent()
  // loanEth.userInfos()
  // eth.getTokenName()
}

initData()
// 定时循环获取单聊数据
const intTime = setInterval(() => {
  loanEth.getPrice()
}, 5 * 1000 * 60)
onUnmounted(() => {
  clearInterval(intTime)
})
onMounted(() => {
  fn();
});
</script>
<style>
#app_box{
}
</style>
